package com.fujimana.android.manacode.loalder;

import java.io.IOException;
import java.util.List;

public abstract class Sources {

	private String encode;

	private String file;

	private Filter filter;

	protected Sources(String file, String encode) {
		this.file = file;

		if (encode == null || encode.trim().equals("")) {
			encode = System.getProperty("file.encoding");
		}

		encode = System.getProperty("file.encoding");

		if (encode == null || encode.trim().equals("")) {
			encode = "UTF-8";
		}

		this.encode = encode;
	}

	public Sources(String file) {
		this(file, null);
	}

	public String getSource(String fileName) throws IOException {
		byte[] data = getData(fileName);

		return new String(data, this.encode);
	}

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

	public Filter getFilter() {
		return filter;
	}

	public void setFilter(Filter filter) {
		this.filter = filter;
	}

	public String getFile() {
		return file;
	}

	protected boolean filtering(String key) {
		if (this.filter == null) {
			return true;
		}

		return this.filter.filter(key);
	}

	public abstract byte[] getData(String entry) throws IOException;

	public abstract void read() throws IOException;

	public abstract List<String> getEntries();
}
